package cn.fastprint;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.print.PrintManager;

import java.util.ArrayList;
import java.util.List;

public class ReceiveActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action)&&type!=null){
            if(type.startsWith("image/")){
                dealPicStream(intent);
            }
        }else if (Intent.ACTION_SEND_MULTIPLE.equals(action)&&type!=null){
            if (type.startsWith("image/")){
                dealMultiplePicStream(intent);
            }
        }
    }
    void dealPicStream(Intent intent){
        Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
        System.out.println("uri:"+uri);
        List<Uri> list=new ArrayList<>();
        list.add(uri);
        doPrint(list);
    }

    void dealMultiplePicStream(Intent intent){
        List<Uri> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        for(Uri uri: list){
            System.out.println("multiple uri:"+uri);
        }
        doPrint(list);
    }
    private void doPrint(List<Uri> list) {
        // Get a PrintManager instance
        PrintManager printManager = (PrintManager)this.getSystemService(Context.PRINT_SERVICE);

        // Set job name, which will be displayed in the print queue
        String jobName = this.getString(R.string.app_name) + " Document";

        // Start a print job, passing in a PrintDocumentAdapter implementation
        // to handle the generation of a print document
        printManager.print(jobName, new MyPrintDocumentAdapter(this,list),
                null); //
    }
}
